package com.byq.mm.gateway;

import java.util.ArrayList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.Toast;


import com.byq.mm.gateway.adapter.HomeAdapter;

import com.tc.gateway.device.AbstractDevice;
import com.tc.gateway.event.GroupAddRespEvent;



public class DeviceManageActivity extends Activity{
	AssistanceApplication application;
	
    GridView deviceManageGrid;
    BroadcastReceiver messageReceiver;
   
    void updateAll(){
    	updateGrid(deviceManageGrid);
 
    }
    void updateGrid(GridView v){
    	HomeAdapter ad = (HomeAdapter)v.getAdapter();
    	ad.reset();
    	v.setAdapter(ad);
    	
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   WindowManager.LayoutParams.FLAG_FULLSCREEN);  
        setContentView(R.layout.devicemanage);
       
        deviceManageGrid = (GridView) findViewById(R.id.deviceManageGrid);
       
        application = (AssistanceApplication)this.getApplication();
		
       
        	deviceManageGrid.setAdapter(new HomeAdapter(application, this){

				@Override
				public boolean action(int position) {
					AbstractDevice e = (AbstractDevice)this.getItem(position);

					Intent intent = new Intent(); 
					intent.putExtra("id", e.getId());
					intent.setClass(ctx,DeviceEditActivity.class);
		            ctx.startActivity(intent); 
		            ctx.overridePendingTransition(R.anim.zoom_enter,
		                    R.anim.hold);
			        return false;

				}

				@Override
				public void fetch() {
					AndroidHomeApplication ha = application.getHomeApplication();
					devices = new ArrayList(ha.getGatewayEngine().getDeviceManager().getDevices());
				}
        		
        	});
        	

        messageReceiver = new BroadcastReceiver(){

    		@Override
    		public void onReceive(Context context, Intent intent) {
    			
    			if(intent.getAction().equals(AssistanceAction.ACTION_HOME_UPDATE)){
    				updateAll();
    			}
    			if(intent.getAction().equals(AssistanceAction.ACTION_DEVICE_CONFIG_UPDATE)){
    				updateGrid(deviceManageGrid);
    			}
    			if(intent.getAction().equals(AssistanceAction.ACTION_HOME_ERROR)){
    				Toast.makeText(DeviceManageActivity.this, "nnnnn", Toast.LENGTH_SHORT).show();
    				updateAll();
    			}
    			
    		}
    		
    	};
       
                
                IntentFilter mf = new IntentFilter();
               
        		mf.addAction(AssistanceAction.ACTION_HOME_UPDATE);
        		mf.addAction(AssistanceAction.ACTION_HOME_ERROR);
        		mf.addAction(AssistanceAction.ACTION_DEVICE_CONFIG_UPDATE);
        		registerReceiver(messageReceiver, mf);	
        		
        		
        		
    }
	
  

  
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(messageReceiver == null)
			return;
		this.unregisterReceiver(messageReceiver);
		this.messageReceiver = null;
	}
	
}
